<?php
class AjaxController extends ControllerBase 
{
    /**
    * Serve a RESTful web service request
    */
    public function actionIndex() {
        //TODO: Use http://code.google.com/p/oauth-php/ for OAuth
    }
    
    /**
    * Serve an Ajax request of a service
    */
    public function actionService() {
        if (!Yii::app()->request->IsAjaxRequest)
            throw new CHttpException(400, 'Not an Ajax request.');

        if (count($_POST))
            $data = $_POST;
        else
            $data = $_GET;
        
        $sid = $data['serviceId'];
        $result = WCore::service($sid, $data);
        
        if (isset($data['FORMAT']) && $data['FORMAT'] == 'text')
            echo $result->toText();
        else
            echo $result->toJson($this->post('SIMPLIFIED',0));
    }
    
    /**
    * Serve an widget for an Ajax request
    */
    public function actionWidget() {
        if (!Yii::app()->request->IsAjaxRequest)
            throw new CHttpException(400, 'Not an Ajax request.');

        if (count($_POST))
            $data = $_POST;
        else
            $data = $_GET;
        
        $wid = $data['widgetId'];
        unset($data['widgetId']);
        echo $this->widget($wid, $data, TRUE);
    }
}
?>